#include "rkmedia_function_register.h"

videoQueue *high_video_queue = NULL;
videoQueue *low_video_queue = NULL;
rkmediaInfo *rkmedia_config = NULL;
containerInfo *above_container_func = NULL;
audioQueue *audio_queue = NULL;

pthread_mutex_t push_mutex;

int main(int argc, char *argv[])
{
    if (argc != 5)
    {
        cout << "Please Input ./rv1126_ffmpeg_main high_stream_type high_url_address low_stream_type low_url_address. Notice URL_TYPE: 0-->FLV" << endl;
        return -1;
    }

    pthread_mutex_init(&push_mutex, NULL);

    int ret = -1;
    high_video_queue = new videoQueue();
    low_video_queue = new videoQueue();
    rkmedia_config = new rkmediaInfo();
    above_container_func = new containerInfo();
    audio_queue = new audioQueue();

    if (rkmedia_config->rkmedia_module_init())
    {
        printf("rkmedia module init failed...\n");
        return -1;
    }
    else
    {
        printf("rkmedia module init success...\n");
    }

    if (rkmedia_function_achieve(atoi(argv[1]), argv[2], atoi(argv[3]), argv[4]))
    {
        printf("rkmedia module achieve failed...\n");
        return -1;
    }
    else
    {
        printf("rkmedia module achieve success...\n");
    }
    while (1)
    {
        usleep(500000);
    }

    MPP_CHN_S stViChn;
    MPP_CHN_S stVencChn;
    MPP_CHN_S stRgaChn;

    RV1126_VI_CONTAINER vi_container;
    above_container_func->get_vi_container(0, &vi_container);
    stViChn.enModId = RK_ID_VI;
    stViChn.s32ChnId = vi_container.vi_chn;
    //stViChn.s32DevId = CAMERA_ID;

    RV1126_VENC_CONTAINER venc_container;
    above_container_func->get_venc_container(0, &venc_container);
    stVencChn.enModId = RK_ID_VENC;
    stVencChn.s32ChnId = venc_container.venc_chn;
    //stVencChn.s32DevId = 0;
    ret = RK_MPI_SYS_UnBind(&stViChn, &stVencChn);
    if (ret)
    {
        printf("UnBind venc chn failed...\n");
        return -1;
    }
    else
    {
        printf("UnBind venc chn success...\n");
    }

    RV1126_VENC_CONTAINER low_venc_container;
    above_container_func->get_venc_container(1, &low_venc_container);
    RV1126_RGA_CONTAINER rga_container;
    above_container_func->get_rga_container(0, &rga_container);
    stRgaChn.enModId = RK_ID_RGA;
    stRgaChn.s32ChnId = rga_container.rga_chn;
    ret = RK_MPI_SYS_UnBind(&stViChn, &stRgaChn);
    if (ret)
    {
        printf("UnBind rga chn failed...\n");
        return -1;
    }
    else
    {
        printf("UnBind rga chn success...\n");
    }

    MPP_CHN_S stAiChn;
    MPP_CHN_S stAencChn;
    RV1126_AI_CONTAINER ai_container;
    RV1126_AENC_CONTAINER aenc_container;
    above_container_func->get_ai_container(0, &ai_container);
    above_container_func->get_aenc_container(0, &aenc_container);
    stAiChn.enModId = RK_ID_AI;
    stAiChn.s32ChnId = ai_container.ai_chn;
    stAencChn.enModId = RK_ID_AENC;
    stAencChn.s32ChnId = aenc_container.aenc_chn;
    ret = RK_MPI_SYS_UnBind(&stAiChn, &stAencChn);
    if (ret)
    {
        printf("UnBind aenc chn failed...\n");
        return -1;
    }
    else
    {
        printf("UnBind aenc chn success...\n");
    }
    RK_MPI_VENC_DestroyChn(venc_container.venc_chn);
    RK_MPI_VENC_DestroyChn(low_venc_container.venc_chn);
    RK_MPI_RGA_DestroyChn(rga_container.rga_chn);
    RK_MPI_AENC_DestroyChn(aenc_container.aenc_chn);
    RK_MPI_VI_DisableChn(CAMERA_ID, vi_container.vi_chn);
    RK_MPI_AI_DisableChn(ai_container.ai_chn);

    pthread_mutex_destroy(&push_mutex);
  
    delete high_video_queue;
    delete low_video_queue;
    delete rkmedia_config;
    delete above_container_func;
    delete audio_queue;

    return 0;
}
